﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sys_databack : System.Web.UI.Page
{
    public string BakFileName = "", DataBaseName = "", strLink = "";
    public string BakFileName_ls1 = "", BakFileName_ls2 = "";
    PM.PMSys.log Log = new PM.PMSys.log();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox1.Text = "PDMIS_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".dat";
            this.btnDownload.Visible = false;
        }
    }
    protected void btnBackUp_Click(object sender, EventArgs e)
    {
        this.btnDownload.Visible = true;
        BakFileName = Server.MapPath("../DataBackUp/") + TextBox1.Text.ToString().Trim();      //备份目标文件名
        DataBaseName = "PDMIS";                                                               //需要备份的数据库名称
        if (System.IO.File.Exists(BakFileName))
        {
            BakFileName_ls1 = TextBox1.Text.ToString().Trim();
            BakFileName_ls2 = "PDMIS_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".dat";
            TextBox1.Text = BakFileName_ls2;
            labNote.Text = "已经存在此文件：" + BakFileName_ls1 + "，备份文件已经被自动改名为：" + BakFileName_ls2 + "请重新备份！";
            labNote.Visible = true;
            return;
        }
        try
        {
            Log.DataBackUp(DataBaseName,BakFileName);
            //写日志文件
            if (Session["name"].ToString() != null)
                Log.Insert_Log("备份数据库PDMIS！", 5);
            TextBox1.Enabled = false;
            this.labNote.Text = "备份成功，服务器备份文件为：" + BakFileName;
            this.btnDownload.Visible = true;
            //Session["Msg"] = "<script>alert('备份成功，请及时将备份文件异地再次备份！');</script>";
        }
        catch (Exception errorMsg)
        {
            throw errorMsg;
        }

    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string host_ip = HttpContext.Current.Request.Url.Host.ToString().Trim();      //得到当前的域名或IP地址
        //string filename = "http://" + host_ip + "/UserFiles/" + TextBox1.Text.ToString().Trim();
        string filename = "http://";
        filename += HttpContext.Current.Request.Url.Host.ToString().Trim();        //得到域名或IP
        filename += ":";
        filename += HttpContext.Current.Request.Url.Port.ToString().Trim();        //得到端口号
        string floder = HttpRuntime.AppDomainAppVirtualPath.ToString().Trim();                    //得到虚拟目录
        if (floder != "/")
        {
            filename += floder + "/";
        }
        else
        {
            filename += "/";
        }
        filename += "DataBackUp/" + TextBox1.Text.ToString().Trim();
        strLink = filename;
        Response.Redirect(strLink, false);
    }
}
